﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace GadgeteerBee
{
    public partial class Program
    {
        Module.DisplayModule.SimpleGraphicsInterface sg;
        uint height;
        uint width;
        uint y;
        uint thickness;
        uint offset;

        uint potiX;
        uint potiY;
        uint tempX;
        uint tempY;

        void ProgramStarted()
        {
            display.SimpleGraphics.AutoRedraw = true;

            display.WPFWindow.Invalidate();

            System.Threading.Thread startThread = new Thread(StartDisplay);

            InitializeSerialPort();
            startThread.Start();

            Debug.Print("Program Started");
        }

        void StartDisplay()
        {
            Thread.Sleep(500);

            height = display.Height / 4;
            width = display.Width / 2;
            y = height * 3;
            thickness = 2;
            offset = thickness / 2;

            potiX = tempX = display.Width / 2 - 15;
            tempY = display.Height / 4 - 10;
            potiY = display.Height / 2 - 20;
            
            sg = display.SimpleGraphics;

            sg.Clear();
            sg.DisplayRectangle(Colors.Green, thickness, Colors.Black, 0, y, width - offset, height);
            sg.DisplayRectangle(Colors.Red, thickness, Colors.Black, width + offset, y, width - offset, height);

            DisplayTemperature("TEMP");
            DisplayPoti("POTI");
        }

        object DisplayTemperature(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                DisplayClear(tempX, tempY);
                sg.DisplayText(text, Resources.GetFont(Resources.FontResources.NinaB),
                    Colors.Yellow, tempX, tempY);
            }
            return null;
        }

        object DisplayPoti(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                DisplayClear(potiX, potiY);
                sg.DisplayText(text, Resources.GetFont(Resources.FontResources.NinaB),
                    Colors.Cyan, potiX, potiY);
            }
            return null;
        }

        object DisplayDebug(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                DisplayClear(5, 5);
                sg.DisplayText(text, Resources.GetFont(Resources.FontResources.NinaB),
                    Colors.White, 5, 5);
            }
            return null;
        }

        void DisplayClear(uint x, uint y)
        {
            sg.DisplayRectangle(Colors.Black, thickness, Colors.Black, x, y, 60, 16);
        }
    }
}
